/**
 * Created by Lilinjun on 17/5/25.
 */

$(document).ready(function(){
    List();
});
var fields  = new  Array('cname','cnumber','cobject','bak','cid');

//展示页面
function  List(){
    var selefrom = "";
    $.ajax({
        type:"post",
        url:"/scene/query",
        data:selefrom,
        dataType: "json",
        success:function(data){
            var result = data.data.result;
            var str ="";
            for (var i = 0; i < result.length; i++) {
                str+="<tr>";
                str+="<td><input  type='checkbox'  /></td>";
                for(x in fields){
                    for (var key in result[i]){
                        if(fields[x]==key){
                            str+="<td>"+result[i][key]+"</td>";
                        }
                    }
                }
                str+="<td> <button  class='btn btn-warning btn-xs' onclick='toupdate(this)'>修改</button> </td>"
            }
            str+="<tr>";
            str+="<td></td><td></td><td></td></td><td><td></td></td><td><button  class='btn btn-warning btn-xs' onclick='toupdate(this)'>增加</button> </td>";
            $("#tbody_1").html(str);
        },
        error:function(){
            alert("报错啦");
        }
    })
}






function add(){

    $.ajax({
        type:"post",
        url:"/scene/add",
        async:false,
        data:sceneInfo,
        success:function(){
            alert("保存成功");
            List();
        }
    });

}




function toupdate(btn){

    if(btn.innerHTML=="确认修改"){
        var tr = btn.parentElement.parentElement;
        //'cname','cnumber','cobject','bak','cid'
        var cname = tr.cells[1];
        var cnumber=tr.cells[2];
        var cobject = tr.cells[3];
        var bak=tr.cells[4];
        var  cid=tr.cells[5];
        var sceneInfo={cid:cid.firstChild.value ,cnumber:cnumber.firstChild.value, cname:cname.firstChild.value, cobject:cobject.firstChild.value ,bak:bak.firstChild.value};
        console.log(sceneInfo);
        $.ajax({
            type:"post",
            url:"/scene/update",
            async:false,
            data:sceneInfo,
            success:function(){
                alert("保存成功");
                List();
                btn.innerHTML=="修改"
            }
        });
    } else{
        var tr = btn.parentElement.parentElement;

        for(var x=1 ;x<=fields.length;x++){
            if(x==2){
                continue;
            }
            var td = tr.cells[x];
            var txt = document.createElement("input");
            txt.type="text";
            txt.value = td.innerHTML;
            td.innerHTML = "";
            td.appendChild(txt);
        }


        btn.innerHTML="确认修改";
    }
}


